package com.yisu.auction.api.web.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.DoubleArraySerializer;
import com.yisu.auction.api.domain.vo.mobileBelongToPackgeVos.MobileBelongToBackVo;
import com.yisu.auction.api.domain.vo.mobileBelongToPackgeVos.MobileBelongToVo;
import com.yisu.auction.ewf.common.web.HttpWebClient;
import com.yisu.auction.ewf.entity.MobileRecharge;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by caeri on 2017/9/26.
 */
public class MobileBelongToUtil {

    private static final String url = "http://apis.juhe.cn/mobile/get";

    private static final String appKey = "c0e1a8f90b69f42a3b160fefef2dcc89";

    private static final String mobileHead = "133、153、180、181、189、177、173、149、130、131、132、155、156、145、185、186、176、175、134、135、136、137、138、139、150、151、152、157、158、159、182、183、184、187、188、147、178";

    /**
     * 获取手机所在地等信息
     * @param mobile
     * @param supportProvince
     * @return
     */
    public static MobileBelongToBackVo getMobileBelongTo(String mobile, String supportProvince) {
        MobileBelongToBackVo mobileBelongToBackVo = new MobileBelongToBackVo();
        if (mobile == null || mobile == "") {
            mobileBelongToBackVo.setUserState(0);
            mobileBelongToBackVo.setBelongToText("");
            return mobileBelongToBackVo;
        }
        if (!checkMobile(mobile)) {
            mobileBelongToBackVo.setUserState(0);
            mobileBelongToBackVo.setBelongToText("手机号码不正确");
            return mobileBelongToBackVo;
        }
        String s = HttpWebClient.get(url + "?phone=" + mobile + "&key=" + appKey);
        MobileBelongToVo mobileBelongToVo = JSON.parseObject(s, MobileBelongToVo.class);
        if (mobileBelongToVo.getResultcode() != 200) {
            mobileBelongToBackVo.setUserState(0);
            mobileBelongToBackVo.setBelongToText("手机号码不正确");
            return mobileBelongToBackVo;
        } else {
            if (supportProvince.contains(mobileBelongToVo.getResult().getProvince())) {
                mobileBelongToBackVo.setUserState(1);
                mobileBelongToBackVo.setBelongToText("(" + mobileBelongToVo.getResult().getProvince() + mobileBelongToVo.getResult().getCompany() + ")");
                return mobileBelongToBackVo;
            } else {
                mobileBelongToBackVo.setUserState(0);
                mobileBelongToBackVo.setBelongToText("(" + mobileBelongToVo.getResult().getProvince() + mobileBelongToVo.getResult().getCompany() + ")");
                return mobileBelongToBackVo;
            }
        }
    }

    /**
     * 检测手机号是否正确
     * @param mobile
     * @return
     */
    public static boolean checkMobile(String mobile) {
        if (mobile == null || mobile == "") {
            return true;
        }
        if (mobile.replaceAll(" ", "").length() != 11) {
            return false;
        }
        if (!StringUtils.isNumeric(mobile)) {
            return false;
        }
        if (mobileHead.indexOf(mobile.substring(0, 3)) == -1) {
            return false;
        }
        return true;
    }

    /**
     * 获取手机运营商
     * @param mobile
     * @return
     */
    public static int getMobileOperator(String mobile) {
        if (mobile == null || mobile.isEmpty()) {
            return 0;
        }
        String json = HttpWebClient.get(url + "?phone=" + mobile + "&key=" + appKey);
        MobileBelongToVo mobileBelongToVo = JSON.parseObject(json, MobileBelongToVo.class);
        String company = mobileBelongToVo.getResult().getCompany();
        if (company.indexOf("移动") != -1) {
            return 1;
        } else if (company.indexOf("联通") != -1) {
            return 2;
        } else {
            return 3;
        }
    }
}
